Imports ToolKitVB.Web.UI.Javascript
Imports System.ComponentModel
Imports System.Web.UI

<Bindable(True), _
 System.Drawing.ToolboxBitmap(GetType(ToolKitDropDownList), _
"Web.UI.WebControls.DropDownList.ico")> _
Public Class ToolKitDropDownList
    Inherits WebControls.DropDownList
    Implements IToolKit

    Dim _databound As Boolean = False

    Public Sub New()
        MyBase.New()
    End Sub
    Private _AddToJavascript As Boolean = False
    ''' <summary>
    ''' Makes TextBox Available in AddToJavascript.
    ''' </summary>
    ''' <remarks></remarks>
    Public Property AddToJavascript() As Boolean
        Get
            Return _AddToJavascript
        End Get
        Set(ByVal value As Boolean)
            _AddToJavascript = value
        End Set
    End Property
    Private _OnClientChange As String = ""
    Public Property OnClientChange() As String
        Get
            Return _OnClientChange
        End Get
        Set(ByVal value As String)
            _OnClientChange = value
        End Set
    End Property
    Public Property ToolKitPage() As Web.UI.ToolKitPage
        Get
            Return DirectCast(MyBase.Page, Web.UI.ToolKitPage)
        End Get
        Set(ByVal value As Web.UI.ToolKitPage)
            MyBase.Page = value
        End Set
    End Property
    <Bindable(BindableSupport.Yes, BindingDirection.TwoWay)> _
    Public Overrides Property SelectedValue() As String
        Get
            If MyBase.SelectedValue Is Nothing Then MyBase.SelectedIndex = 0
            Return MyBase.SelectedValue
        End Get
        Set(ByVal value As String)
            MyBase.SelectedValue = value
        End Set
    End Property
    <Bindable(BindableSupport.Yes, BindingDirection.TwoWay)> _
    Public Overrides Property SelectedIndex() As Integer
        Get
            Return MyBase.SelectedIndex
        End Get
        Set(ByVal value As Integer)
            MyBase.SelectedIndex = value
        End Set
    End Property
    <Bindable(BindableSupport.Yes, BindingDirection.TwoWay)> _
    Public Overrides Property Text() As String
        Get
            Return MyBase.Text
        End Get
        Set(ByVal value As String)
            MyBase.Text = value
        End Set
    End Property

    Enum EmptyValueEnum
        Never
        UnselectedOnly
        Always
    End Enum
    Private _EmptyValue As EmptyValueEnum
    Public Property EmptyValue() As EmptyValueEnum
        Get
            Return _EmptyValue
        End Get
        Set(ByVal value As EmptyValueEnum)
            _EmptyValue = value
        End Set
    End Property
    Public Overrides ReadOnly Property SelectedItem() As System.Web.UI.WebControls.ListItem
        Get
            Return MyBase.SelectedItem
        End Get
    End Property
    Protected Overrides Sub OnPreRender(ByVal e As System.EventArgs)
        MyBase.OnPreRender(e)
        If _AddToJavascript Then
            Me.ToolKitPage.Javascript.RegisterControl(Me)
        End If
        If _OnClientChange <> "" Then Me.Attributes.Add("onchange", ToolKitTools.WrapErrorHandler(_OnClientChange, Me.ID & " OnClientChange"))
    End Sub

    Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
        MyBase.Render(writer)
    End Sub

    Protected Overrides Sub OnDataBinding(ByVal e As System.EventArgs)
        MyBase.OnDataBinding(e)
    End Sub
    Protected Overrides Sub OnDataBound(ByVal e As System.EventArgs)
        MyBase.OnDataBound(e)
    End Sub
    '<System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="Execution")> _
    'Protected Overrides Sub CreateChildControls()
    '    'If Me.Items.FindByValue("") Is Nothing AndAlso _
    '    '    (EmptyValue = EmptyValueEnum.Always OrElse _
    '    '    (EmptyValue = EmptyValueEnum.UnselectedOnly And Me.SelectedValue = "")) Then _
    '    '        Me.Items.Add(New System.Web.UI.WebControls.ListItem("Please Select", ""))
    '    Me.CreateChildControls()
    'End Sub

    Public Overrides Function ToString() As String Implements IToolKit.ToString
        Return Me.SelectedValue
    End Function
End Class
